package com.assistant.service.impl;

import com.assistant.common.DateUtil;
import com.assistant.mapper.ItemsMapper;
import com.assistant.mapper.PostItemsMapper;
import com.assistant.pojo.Items;
import com.assistant.pojo.ItemsPost;
import com.assistant.service.ItemsPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ItemsPostServiceImpl implements ItemsPostService {
    @Autowired
    private PostItemsMapper postItemsMapper;
    @Autowired
    private ItemsMapper itemsMapper;


    /*
     * 新增 请求代取  //0:下架 1:上架  (已改)
     * */
    public int insertPostItems(Items items) {
        items.setStatus(3);
        items.setPost_time(DateUtil.dataFormatUtil(new Date()));
        return itemsMapper.insertSelective(items);
    }

    /*
     * 更新代取信息  （已改）
     * */
    public int updateItemsMessage(Items items) {
        items.setUpdate_time(DateUtil.dataFormatUtil(new Date()));
        return itemsMapper.updateByPrimaryKeySelective(items);
    }

    /*
     * 商品下架
     * */
    public int cancelPostItems(Long id) {
        Items items = new Items();
        items.setItems_id(id);
        items.setStatus(0);  //取消
        return itemsMapper.updateByPrimaryKeySelective(items);
    }

    /*
     * 查首页、模糊查询物品名、kind。
     * */
    public List<ItemsPost> showItemsMessageByXxx(Map<String, String> map) {
        Example example = new Example(ItemsPost.class);
        Example.Criteria criteria = example.createCriteria();

        if (map.get("status") != null) {
            criteria.andEqualTo("status", map.get("status"));//上架
        }

        if (map.get("items_name") != null) {
            criteria.andLike("items_name", "%" + map.get("items_name") + "%");
        }
        if (map.get("kind") != null) {
            criteria.andLike("kind", "%" + map.get("kind") + "%");
        }
        if (map.get("items_id") != null) {
            criteria.andLike("items_id", map.get("items_id"));
        }
        if (map.get("poster_id") != null) {
            criteria.andLike("poster_id", map.get("poster_id"));
        }
        return postItemsMapper.selectByExample(example);
    }


    /*
     * 文件上传
     * */
    public String upLoadPic(MultipartFile[] files) {
        // 上传简单文件名(原始文件名)
        String originalFilename = files[0].getOriginalFilename();
        // 存储路径（项目路径 ）
        String property = System.getProperty("user.dir");
        String path = property + "/src/main/resources/static/img/" + System.currentTimeMillis();
        String name = path + originalFilename;

        try {
            // 保存文件（将上传文件写到服务器上指定的文件）
            files[0].transferTo(new File(name));

        } catch (IOException e) {
            e.printStackTrace();
        }
        String b = "http://ice2020x.cn.utools.club";
        return b + name.substring(name.lastIndexOf("/") - 4);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        ItemsPost itemsPost = new ItemsPost();
        itemsPost.setItems_id(Long.valueOf(id));
        itemsPost.setStatus(1);
        return postItemsMapper.updateByPrimaryKeySelective(itemsPost);
    }

    @Override
    public int deleteItems(Integer id) {
        return postItemsMapper.deleteByPrimaryKey(id);
    }


}


